from sqlalchemy.orm import Session

from core.entity.do.car_track_do import CarTrack
from core.entity.vo.car_track_vo import *
from utils.time_format_util import list_format_datetime
from datetime import datetime, time


class CarTrackDao:
    """
    参数配置管理模块数据库操作层
    """

    @classmethod
    def get_car_track_by_id(cls, db: Session, id: int):
        """
        根据id查询车辆轨迹信息
        :param db: orm对象
        :param id: 主键ID
        :return: 车辆轨迹信息对象
        """
        car_track = db.query(CarTrack) \
            .filter(CarTrack.id == id) \
            .first()

        return car_track

    @classmethod
    def get_car_track_by_car_id(cls, db: Session, carId: str):
        """
        根据车辆编码查询车辆轨迹信息
        :param db: orm对象
        :param carTrack: 参数配置参数对象
        :return: 参数配置信息对象
        """
        car_track = db.query(CarTrack) \
            .filter(CarTrack.car_id == carId) \
            .all()

        return car_track

    @classmethod
    def get_car_track_by_device_id(cls, db: Session,deviceId: str):
        """
        获取所有的参数配置信息
        :param db: orm对象
        :return: 参数配置信息列表对象
        """
        car_track = db.query(CarTrack) \
            .filter(CarTrack.device_id == deviceId) \
            .all()

        return car_track

    @classmethod
    def add_car_track_dao(cls, db: Session, carTrack: CarTrackModel):
        """
        新增参数配置数据库操作
        :param db: orm对象
        :param config: 参数配置对象
        :return:
        """
        car_track = CarTrack(**carTrack.dict())
        db.add(car_track)
        db.flush()

        return car_track

    @classmethod
    def edit_car_track_dao(cls, db: Session, carTrack: dict):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param config: 需要更新的参数配置字典
        :return:
        """
        db.query(CarTrack) \
            .filter(CarTrack.id == carTrack.get('id')) \
            .update(carTrack)

    @classmethod
    def delete_car_track_dao(cls, db: Session, id: int):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param config: 需要更新的参数配置字典
        :return:
        """
        db.query(CarTrack) \
            .filter(CarTrack.id == id) \
            .delete()
